package com.ssd.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssd.entity.SkuEntity;
import org.apache.ibatis.annotations.*;

import java.util.Map;

@Mapper
public interface SkuDao extends BaseMapper<SkuEntity> {
    @Select("<script>" +
            "SELECT t1.*, t2.class_name " +
            "FROM tb_sku t1 " +
            "LEFT JOIN tb_sku_class t2 ON t1.class_id = t2.class_id " +
            "<if test='innerCode != null and innerCode != \"\"'> " +
            "LEFT JOIN tb_channel t3 ON t1.sku_id = t3.sku_id AND t3.inner_code = #{innerCode} " +
            "</if> " +
            "<where> " +
            "<if test='skuName != null and skuName != \"\"'> " +
            "AND t1.sku_name LIKE CONCAT('%', #{skuName}, '%') " +
            "</if> " +
            "<if test='innerCode != null and innerCode != \"\"'> " +
            "AND t3.inner_code IS NOT NULL " +
            "</if> " +
            "</where> " +
            "ORDER BY t1.sku_id " +
            "</script>")
    @Results({
            @Result(property="skuId", column="sku_id"),
            @Result(property="skuName", column="sku_name"),
            @Result(property="skuImage", column="sku_image"),
            @Result(property="price", column="price"),
            @Result(property="classId", column="class_id"),
            @Result(property="discount", column="is_discount"),
            @Result(property="unit", column="unit"),
            @Result(property="brandName", column="brand_name"),
            @Result(property="className", column="class_name"),
            @Result(property="createTime", column="create_time")
    })
    Page<Map<String, Object>> selectSkuWithClass(Page<?> page, String skuName, String innerCode);
    /**
     * 新增商品信息
     *
     * @param sku 商品信息
     * @return 结果
     */
    @Insert("INSERT INTO tb_sku (sku_id, sku_name, sku_image, price, class_id, is_discount, unit, brand_name) " +
            "VALUES (#{skuId}, #{skuName}, #{skuImage}, #{price}, #{classId}, #{discount}, #{unit}, #{brandName})")
    int insertSku(SkuEntity sku);

    /**
     * 修改商品信息
     *
     * @param sku 商品信息
     * @return 结果
     */
    @Update("UPDATE tb_sku SET sku_name=#{skuName}, sku_image=#{skuImage}, price=#{price}, " +
            "class_id=#{classId}, is_discount=#{discount}, unit=#{unit}, brand_name=#{brandName} " +
            "WHERE sku_id=#{skuId}")
    int updateSku(SkuEntity sku);

    /**
     * 删除商品信息
     *
     * @param skuId 商品主键
     * @return 结果
     */
    @Delete("DELETE FROM tb_sku WHERE sku_id = #{skuId}")
    int deleteSkuBySkuId(Long skuId);

    /**
     * 批量删除商品信息
     *
     * @param skuIds 需要删除的商品主键集合
     * @return 结果
     */
    @Delete("<script>" +
            "DELETE FROM tb_sku" +
            "<where>" +
            "sku_id IN" +
            "<foreach item='id' index='index' collection='array' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</where>" +
            "</script>")
    int deleteSkuBySkuIds(Long[] skuIds);

    /**
     * 根据商品ID查询商品详情
     *
     * @param skuId 商品主键
     * @return 商品详情
     */
    @Select("SELECT t1.*, t2.class_name " +
            "FROM tb_sku t1 " +
            "LEFT JOIN tb_sku_class t2 ON t1.class_id = t2.class_id " +
            "WHERE t1.sku_id = #{skuId}")
    @Results({
            @Result(property="skuId", column="sku_id"),
            @Result(property="skuName", column="sku_name"),
            @Result(property="skuImage", column="sku_image"),
            @Result(property="price", column="price"),
            @Result(property="classId", column="class_id"),
            @Result(property="discount", column="is_discount"),
            @Result(property="unit", column="unit"),
            @Result(property="brandName", column="brand_name"),
            @Result(property="className", column="class_name"),
            @Result(property="createTime", column="create_time")
    })
    SkuEntity selectSkuBySkuId(Long skuId);
}
